package com.ourteam.product.api;

import java.util.Date;

import org.apache.commons.lang3.ArrayUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ourteam.frame.controller.AbstractCRUDServiceController;
import com.ourteam.frame.web.OperationResult;
import com.ourteam.product.ui.dao.ProductUIActionGroupRelate;
import com.ourteam.product.ui.dao.ProductUIActionGroupRelateDAOQueryBean;
import com.ourteam.product.ui.domain.ProductUIActionGroupRelateBean;
import com.ourteam.product.ui.service.IProductUIService;
import com.ourteam.product.ui.service.ProductUIServiceFactory;

@Controller
@CrossOrigin
@RequestMapping("/api/PRODUCT/UI/productUIActionGroupRelate")
public class ProductUIActionGroupRelateAPI extends
		AbstractCRUDServiceController<ProductUIActionGroupRelateDAOQueryBean, ProductUIActionGroupRelateSearchModel, ProductUIActionGroupRelateBean> {

	private static final IProductUIService PRODUCT_UI_SERVICE = ProductUIServiceFactory.getProductUIService();

	@Override
	protected ProductUIActionGroupRelateDAOQueryBean newQueryBean() {
		return new ProductUIActionGroupRelateDAOQueryBean();
	}

	@Override
	protected int queryCount(ProductUIActionGroupRelateDAOQueryBean queryBean) throws Exception {
		return PRODUCT_UI_SERVICE.queryProductUIActionGroupRelateCount(queryBean);
	}

	@Override
	protected ProductUIActionGroupRelateBean[] queryData(ProductUIActionGroupRelateDAOQueryBean queryBean)
			throws Exception {
		return PRODUCT_UI_SERVICE.queryProductUIActionGroupRelates(queryBean);
	}

	@Override
	protected void deleteData(long[] ids) throws Exception {
		PRODUCT_UI_SERVICE.deleteProductUIActionGroupRelates(ids);
	}

	@Override
	protected ProductUIActionGroupRelateBean getDataById(long id) throws Exception {
		return PRODUCT_UI_SERVICE.getProductUIActionGroupRelateById(id);
	}

	@Override
	protected void saveData(ProductUIActionGroupRelateBean dataBean) throws Exception {
		dataBean.setLastModifyTime(new Date());
		dataBean.setLastModifyUser(getLoginUserName());
		if (dataBean.getId() == 0) {
			PRODUCT_UI_SERVICE.addNewProductUIActionGroupRelate(dataBean);
		} else {
			PRODUCT_UI_SERVICE.modifyProductUIActionGroupRelate(dataBean);
		}
	}

	@RequestMapping("/addUIActionRelate")
	@ResponseBody
	public OperationResult addUIActionRelate(final @RequestParam long actionGroupId,
			final @RequestParam Long[] relatedActionIds) throws Exception {
		
		if(ArrayUtils.isEmpty(relatedActionIds)) {
			return OperationResult.SUCCESS_RESULT;
		}
		
		String operatorName = getLoginUserName();
		
		Date operationTime = new Date();
		
		
		for (int i = 0; i < relatedActionIds.length; i++) {
			
			ProductUIActionGroupRelateBean relateBean = new ProductUIActionGroupRelateBean();
			
			relateBean.setActionGroupId(actionGroupId);
			
			relateBean.setRelatedActionId(relatedActionIds[i]);
			
			relateBean.setLastModifyUser(operatorName);
			
			relateBean.setLastModifyTime(operationTime);
			
			PRODUCT_UI_SERVICE.addNewProductUIActionGroupRelate(relateBean);
		}

		return OperationResult.SUCCESS_RESULT;

	}

}
